<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/12
 * Time: 9:26
 */

namespace common\business\kd;


use common\consts\Consts;
use common\consts\MdmConsts;
use common\helpers\StringUtil;
use yii\helpers\Json;

class LoginApiHandler
{
    const wsdl_uri = 'http://hr.vpn.stec.net:6888/ormrpc/services/EASLogin?wsdl';

    /**
     * @param $username string
     * @param $password string
     * @return string
     */
    public function webServiceCallSync($username, $password){
        try {
            $client = new \SoapClient(self::wsdl_uri);

            //要向服务端要传的参数
            $loginInfo = array(
                'userName' => $username,
                'password' => $password,
                'slnName' => 'eas',
                'dcName' => 'Stechr',
                'language' => '12',
                'dbType' => 1,
            );

            //调用服务端的方法
            $result = $client->__soapCall('login', $loginInfo);

//            $result = $client->login([
//                'loginRequest' => $loginInfo,
//            ]);
//            return $result->return;

            if (!StringUtil::isNullOrWhiteSpace($result->sessionId)){
                $resultArr = [
                    MdmConsts::RESULT => Consts::YES,
                    MdmConsts::MESSAGE => 'Login Success',
                    'sessionId' => $result->sessionId,
                ];
            }
            else {
                $resultArr = [
                    MdmConsts::RESULT => Consts::NO,
                    MdmConsts::MESSAGE => 'Login Failed',
                    'sessionId' => '',
                ];
            }
            return Json::encode($resultArr);
        } catch (\Exception $e) {
            $exceptionResultArr = [
                MdmConsts::RESULT => Consts::NO,
                MdmConsts::MESSAGE => $e->getMessage(),
            ];
            return Json::encode($exceptionResultArr);
        }
    }
}